package org.blog.service.impl;

import org.blog.dao.ArticleMapper;
import org.blog.dao.MessageMapper;
import org.blog.dao.UserMapper;
import org.blog.entity.Article;
import org.blog.entity.User;
import org.blog.entity.param.ArticleParam;
import org.blog.entity.param.UserParam;
import org.blog.entity.vo.MessageVO;
import org.blog.entity.vo.UserCenterVO;
import org.blog.service.ArticleService;
import org.blog.service.UserService;
import org.blog.utils.ObjectToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private ArticleMapper articleMapper;


    @Override
    public boolean add(User user) {
        System.out.println(user.toString());
        int i = userMapper.insertSelective(user);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<User> search(User user) {
        return null;
    }

    @Override
    public boolean delete(Integer userId) {
        int i = userMapper.deleteByPrimaryKey(userId);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean update(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public User selectByUserName(String userName) {

        User user = userMapper.selectByUserName(userName);
        return user;
    }

    @Override
    public UserCenterVO userCenter(Integer userId) {

        List<MessageVO> messageVOs = messageMapper.getMessageVOs();
        UserCenterVO userCenterVO = userMapper.userCenter(userId);
        ArticleParam articleParam = new ArticleParam();
        articleParam.setUserId(userId);
        List<Article> articles = null;
        try {
            articles = articleMapper.getArticles(ObjectToMap.convertBeanToMap(articleParam));
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        userCenterVO.setMessageVOS(messageVOs);
        userCenterVO.setArticles(articles);

        return userCenterVO;
    }

    @Override
    public List<User> getUsers(UserParam param) {
        Map<String, Object> stringObjectMap = null;
        try {
            stringObjectMap = ObjectToMap.convertBeanToMap(param);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        List<User>  list = userMapper.getUsers(stringObjectMap);
        return list;
    }
}
